/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.workbench.tests.api;

import junit.framework.Assert;

import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.IWorkbenchPart2;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.tests.harness.util.UITestCase;


/**
 * 
 * ViewPartTitleTest.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class ViewPartTitleTest extends UITestCase {
    WorkbenchWindow window;
    WorkbenchPage page;
    EmptyView view;
    IWorkbenchPartReference ref;
    boolean titleChangeEvent = false;
    boolean nameChangeEvent = false;
    boolean contentChangeEvent = false;
    private IPropertyListener propertyListener = new IPropertyListener() {
            /* (non-Javadoc)
             * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
             */
            public void propertyChanged(Object source, int propId) {
                switch (propId) {
                case IWorkbenchPartConstants.PROP_TITLE:
                    titleChangeEvent = true;

                    break;

                case IWorkbenchPartConstants.PROP_PART_NAME:
                    nameChangeEvent = true;

                    break;

                case IWorkbenchPartConstants.PROP_CONTENT_DESCRIPTION:
                    contentChangeEvent = true;

                    break;
                }
            }
        };

    /**
     * @param testName
     */
    public ViewPartTitleTest(String testName) {
        super(testName);
    }

    protected void doSetUp() throws Exception {
        super.doSetUp();
        window = (WorkbenchWindow) openTestWindow();
        page = (WorkbenchPage) window.getActivePage();

        String viewId = "org.eclipse.ui.tests.workbenchpart.EmptyView";
        view = (EmptyView) page.showView(viewId);
        ref = page.findViewReference(viewId);
        view.addPropertyListener(propertyListener);
        titleChangeEvent = false;
        nameChangeEvent = false;
        contentChangeEvent = false;
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.tests.util.UITestCase#doTearDown()
     */
    protected void doTearDown() throws Exception {
        view.removePropertyListener(propertyListener);
        page.hideView(view);
        super.doTearDown();
    }

    private void verifySettings(IWorkbenchPart2 part, String expectedTitle,
        String expectedPartName, String expectedContentDescription)
        throws Exception {
        Assert.assertEquals("Incorrect view title", expectedTitle,
            part.getTitle());
        Assert.assertEquals("Incorrect part name", expectedPartName,
            part.getPartName());
        Assert.assertEquals("Incorrect content description",
            expectedContentDescription, part.getContentDescription());

        Assert.assertEquals("Incorrect title in view reference", expectedTitle,
            ref.getTitle());
        Assert.assertEquals("Incorrect part name in view reference",
            expectedPartName, ref.getPartName());
        Assert.assertEquals("Incorrect content description in view reference",
            expectedContentDescription, ref.getContentDescription());
    }

    private void verifySettings(String expectedTitle, String expectedPartName,
        String expectedContentDescription) throws Exception {
        verifySettings(view, expectedTitle, expectedPartName,
            expectedContentDescription);
    }

    /**
     * Ensure that we've received the given property change events since the start of the test
     *
     * @param titleEvent PROP_TITLE
     * @param nameEvent PROP_PART_NAME
     * @param descriptionEvent PROP_CONTENT_DESCRIPTION
     */
    private void verifyEvents(boolean titleEvent, boolean nameEvent,
        boolean descriptionEvent) {
        if (titleEvent) {
            Assert.assertEquals("Missing title change event", titleEvent,
                titleChangeEvent);
        }

        if (nameEvent) {
            Assert.assertEquals("Missing name change event", nameEvent,
                nameChangeEvent);
        }

        if (descriptionEvent) {
            Assert.assertEquals("Missing content description event",
                descriptionEvent, contentChangeEvent);
        }
    }

    public void testDefaults() throws Throwable {
        verifySettings("EmptyView", "EmptyView", "");
        verifyEvents(false, false, false);
    }

    /**
     * Ensures that we can call ViewPart.setTitle(null) without throwing
     * any exceptions
     *
     * @throws Throwable
     */
    public void testNullTitle() throws Throwable {
        view.setTitle(null);

        verifySettings("", "EmptyView", "");
        verifyEvents(true, false, false);
    }

    public void testCustomName() throws Throwable {
        view.setPartName("CustomPartName");
        verifySettings("CustomPartName", "CustomPartName", "");
        verifyEvents(true, true, false);
    }

    public void testCustomTitle() throws Throwable {
        view.setTitle("CustomTitle");
        verifySettings("CustomTitle", "EmptyView", "CustomTitle");
        verifyEvents(true, false, true);
    }

    public void testCustomContentDescription() throws Throwable {
        view.setContentDescription("CustomContentDescription");
        verifySettings("EmptyView (CustomContentDescription)", "EmptyView",
            "CustomContentDescription");
        verifyEvents(true, false, true);
    }

    public void testCustomNameAndContentDescription() throws Throwable {
        view.setPartName("CustomName");
        view.setContentDescription("CustomContentDescription");
        verifySettings("CustomName (CustomContentDescription)", "CustomName",
            "CustomContentDescription");
        verifyEvents(true, true, true);
    }
}
